/**
 * Copyright (c) 2011, University of North Carolina, Chapel Hill

 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, 
 are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this list of 
     conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice, this list 
     of conditions and the following disclaimer in the documentation and/or other materials 
     provided with the distribution.

 * Neither the name of the University of North Carolina at Chapel Hill nor the names of 
     its contributors may be used to endorse or promote  products derived from this 
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT   
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 */
package edu.unc.ils.memai.extract;

import java.util.ArrayList;


import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import java.util.Set;

import maui.util.Candidate;

public class NASACandidateExtractor extends CandidateExtractor
{
    List<String> stopwords = new ArrayList<String>();

    int pos = 0;
    int totalFrequency = 0;
    int firstWord = 0;
    
    public HashMap<String, Candidate> getCandidates(List<String> segments) 
        throws Exception 
    {

        if (debugMode) {
            System.err.println("---- Extracting candidates... ");
        }

        HashMap<String, Candidate> candidatesTable = new HashMap<String, Candidate>();


        pos = 0;
        totalFrequency = 0;
        firstWord = 0;
        
        for (String segment: segments)
        {
            String[] words = segment.split(" ");
            for (int i=0; i<words.length; i++)
            {
                pos++;
                
                String key = stemmer.stem(words[i]);
                if (!stopwords.contains(words[i]))
                {
                    lookup(candidatesTable, words, key, i, i+maxPhraseLength);
                }
            }
        }
           

        Set<String> keys = new HashSet<String>();
        keys.addAll(candidatesTable.keySet());
        for (String key : keys) {
            Candidate candidate = candidatesTable.get(key);
            if (candidate.getFrequency() < minOccurFrequency)
                candidatesTable.remove(key);
            else
                candidate.normalize(totalFrequency, pos);
        }

        return candidatesTable;
    }
    
    public List<String> lookup(Map<String, Candidate> candidatesTable, 
            String[] words, String prefix, int start, int length)  throws Exception
    {
        List<String> found = new ArrayList<String>();
        
        List<String> ids = vocab.getSenses(prefix);
        if (ids != null)
        {
            found.addAll(ids);
            
            for (String id : ids) 
            {
                Candidate candidate = candidatesTable.get(id);

                if (candidate == null) {
                    // this is the first occurrence of this
                    // candidate create a candidate object

                    firstWord = pos - start;
                    candidate = new Candidate(id, prefix, firstWord);
                    totalFrequency++;
                    
                    candidate.setTitle(vocab.getTerm(id));
                } else {

                    // candidate has been observed before update its values
                    firstWord = pos - start;
                    candidate.recordOccurrence(prefix, firstWord);
                    totalFrequency++;

                }
                if (candidate != null) {
                    candidatesTable.put(id, candidate);
                }
            }         
        }
        
        for (int i=start+1; i<length; i++)
        {
            if (i > words.length-1)
                continue;
            
            if (!stopwords.contains(words[i]))
            {
                String key = prefix + " " + stemmer.stem(words[i]);            
                List<String> f = lookup(candidatesTable, words,key, i,length);
                found.addAll(f);
            }
        }
        return found;
    }
}

